package com.paper.check.algorithm.core;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.paper.check.search.core.model.SearchResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;

import com.paper.check.algorithm.model.SearchPaperIndexVo;

public class SearchRes2IndexUtils {

    private static Logger logger = LoggerFactory.getLogger(SearchRes2IndexUtils.class);

    public static List<SearchPaperIndexVo> search2IndexVo(SearchResponse searchResponse) {
        List<SearchPaperIndexVo> indexVos = new ArrayList<SearchPaperIndexVo>();
        if (searchResponse != null && searchResponse.getDetailList() != null) {
            for (Map<String, Collection<Object>> map : searchResponse.getDetailList()) {
                try {
                    SearchPaperIndexVo indexVo = new SearchPaperIndexVo();
                    Collection<Object> objects = map.get("paperId");
                    if(!CollectionUtils.isEmpty(objects)){
                        for(Object object : objects){
                            String paperId = object.toString();
                            indexVo.setPaperId(paperId);
                            break;
                        }
                    }
                    
                    objects = map.get("content");
                    if(!CollectionUtils.isEmpty(objects)){
                        for(Object object : objects){
                            String content = object.toString();
                            indexVo.setContent(content);
                            break;
                        }
                    }
                    
                    objects = map.get("mysqlId");
                    if(!CollectionUtils.isEmpty(objects)){
                        for(Object object : objects){
                            String mysqlId = object.toString();
                            indexVo.setMysqlId(Long.valueOf(mysqlId));
                            break;
                        }
                    }
                    
                    objects = map.get("mysqlTableName");
                    if(!CollectionUtils.isEmpty(objects)){
                        for(Object object : objects){
                            String mysqlTableName = object.toString();
                            indexVo.setMysqlTableName(mysqlTableName);
                            break;
                        }
                    }
                    indexVos.add(indexVo);
                } catch (Exception e) {
                    logger.error("搜索数据转换出错", e);
                }
            }
        }
        return indexVos;
    }
}
